package com.jdcjk.project.car.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.jdcjk.Constant;
import com.jdcjk.R;
import com.jdcjk.UrlAddress;
import com.jdcjk.base.presenter.BasePresenter;
import com.jdcjk.base.view.BaseActivity;
import com.jdcjk.project.account.model.entity.Result;
import com.jdcjk.project.car.model.entity.TerminalInfoResult;
import com.jdcjk.project.car.presenter.CarPresenter;
import com.jdcjk.project.main.model.entity.CarListInfo;
import com.jdcjk.project.main.view.MainActivity;
import com.jdcjk.view.LoadingDialog;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by Administrator on 2016/12/23 0023.
 * 设备详情界面
 */

public class TerminalDetailsActivity extends BaseActivity<CarPresenter> implements View.OnClickListener {
    //返回button
    private ImageButton back_button;
    //顶栏标题textview
    private TextView barTitle_textview;
    //设备型号textview
    private TextView terminalModel_textview;
    //设备名称edittext
    private EditText terminalName_editText;
    //设备SNtextview
    private TextView terminalSn_textview;
    //设备更新时间textview
    private TextView terminalActivateTime_textview;
    //设备车牌号码editText
    private EditText terminalCarNumber_editText;
    //设备手机卡号editText
    private EditText terminalPhoneCarNumber_editText;
    //设备联系人editText
    private EditText terminalLink_textView;
    //设备联系人电话editText
    private EditText terminalLinkPhone_edittext;
    //修改button
    private Button changeTerminalInfo_btn;
    //跳转至该界面的界面类型
    private String fragmentInfo;
    //车辆id
    private int terminalId;
    //车辆信息
    private CarListInfo carListInfo;

    //加载dialog
    private LoadingDialog loadingDialog;


    @Override
    public int getLayoutId() {
        return R.layout.activity_terminal_details;
    }

    @Override
    public CarPresenter bindPresenter() {
        presenter = new CarPresenter(getBaseContext());
        return presenter;
    }

    @Override
    public void initContentView(Bundle savedInstanceState) {
        back_button = (ImageButton) findViewById(R.id.back_btn);
        back_button.setOnClickListener(this);
        barTitle_textview = (TextView) findViewById(R.id.bar_title_textview);
        barTitle_textview.setText(R.string.terminal_details);
        terminalModel_textview = (TextView) findViewById(R.id.terminal_model_tv);
        terminalName_editText = (EditText) findViewById(R.id.terminal_name_et);
        terminalSn_textview = (TextView) findViewById(R.id.terminal_sn_tv);
        terminalActivateTime_textview = (TextView) findViewById(R.id.terminal_activateTime_tv);
        terminalCarNumber_editText = (EditText) findViewById(R.id.terminal_car_number_et);
        terminalPhoneCarNumber_editText = (EditText) findViewById(R.id.terminal_phone_car_number_et);
        terminalLink_textView = (EditText) findViewById(R.id.terminal_link_et);
        terminalLinkPhone_edittext = (EditText) findViewById(R.id.terminal_link_phone_et);

        changeTerminalInfo_btn = (Button) findViewById(R.id.change_terminal_info_btn);
        changeTerminalInfo_btn.setOnClickListener(this);
        //初始化加载dialog
        loadingDialog = new LoadingDialog(this, R.style.dialogTheme);
    }

    @Override
    public void initData() {
        super.initData();
        terminalId = getIntent().getExtras().getInt(Constant.TerminalInfo.terminalId);
        fragmentInfo = getIntent().getStringExtra(Constant.FragmentInfo.fragment);
        //获得终端信息
        presenter.getTerminal(UrlAddress.GET_TERMINAL, String.valueOf(terminalId), new BasePresenter.OnUIThreadListener<TerminalInfoResult>() {
            @Override
            public void onBefore(Request request, int id) {
                loadingDialog.show();
                loadingDialog.setTextView(R.string.is_loaction);
            }

            @Override
            public void onAfter(int id) {

            }

            @Override
            public void onError(Call call, Exception e, int i) {
                loadingDialog.dismiss();
                Toast.makeText(TerminalDetailsActivity.this, R.string.system_exception, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(TerminalInfoResult result) {
                switch (result.getStatus()) {
                    case "OK":
                        carListInfo = result.getResult();
                        //设备型号
                        terminalModel_textview.setText(carListInfo.getModel());
                        //设备名称
                        terminalName_editText.setText(carListInfo.getDisplayid());
                        //设备SN号
                        terminalSn_textview.setText(carListInfo.getSimNum());
                        //设备有效期
                        terminalActivateTime_textview.setText(carListInfo.getCreatedTime());
                        //设备车牌号码
                        terminalCarNumber_editText.setText(carListInfo.getCarId());
                        //设备手机卡号
                        terminalPhoneCarNumber_editText.setText(carListInfo.getCardNum());
                        //设备联系人
                        terminalLink_textView.setText(carListInfo.getLinkman());
                        //设备联系人电话
                        terminalLinkPhone_edittext.setText(carListInfo.getLinkphone());
                        break;
                    case "FAILED":
                        Toast.makeText(TerminalDetailsActivity.this, result.getMessage(), Toast.LENGTH_SHORT).show();
                        break;
                }
                loadingDialog.dismiss();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_terminal_info_btn:
                //修改设备信息Button点击监听
                final String displayid = terminalName_editText.getText().toString();//设备名称
                final String carId = terminalCarNumber_editText.getText().toString();//设备车牌号
                final String cardNum = terminalPhoneCarNumber_editText.getText().toString();//设备手机卡号
                String linkMan = terminalLink_textView.getText().toString();
                String linkPhone = terminalLinkPhone_edittext.getText().toString();
                if (displayid.equals("")) {
                    Toast.makeText(TerminalDetailsActivity.this, R.string.toast_displayid_null, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (carId.equals("")) {
                    Toast.makeText(TerminalDetailsActivity.this, R.string.toast_carId_null, Toast.LENGTH_SHORT).show();
                    return;
                }
                presenter.updataTerminalInfo(UrlAddress.UPDATA_TERMINAL, Integer.toString(carListInfo.getId()), displayid, carId, cardNum, linkMan, linkPhone, new BasePresenter.OnUIThreadListener<Result>() {
                    @Override
                    public void onBefore(Request request, int id) {

                    }

                    @Override
                    public void onAfter(int id) {

                    }

                    @Override
                    public void onError(Call call, Exception e, int i) {
                        Toast.makeText(TerminalDetailsActivity.this, R.string.toast_updata_fail, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Result result) {
                        switch (result.getStatus()) {
                            case "OK":
                                Toast.makeText(TerminalDetailsActivity.this, R.string.toast_updata_success, Toast.LENGTH_SHORT).show();
                                Bundle bundle = new Bundle();
                                bundle.putString(Constant.TerminalInfo.displayid, displayid);
                                bundle.putString(Constant.TerminalInfo.carId, carId);
                                bundle.putString(Constant.TerminalInfo.cardNum, cardNum);
                                //根据不同的转入界面，返回值到不同的界面
                                switch (fragmentInfo) {
                                    case "car_activity":
                                        setResult(1, new Intent(TerminalDetailsActivity.this, CarActivity.class).putExtras(bundle));
                                        break;
                                    case "main_activity":
                                        setResult(1, new Intent(TerminalDetailsActivity.this, MainActivity.class).putExtras(bundle));
                                        break;
                                }
                                finish();
                                break;
                            case "FAILED":
                                Toast.makeText(TerminalDetailsActivity.this, R.string.toast_updata_fail, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case R.id.back_btn:
                //顶栏返回Button监听
                finish();
                break;
        }
    }
}
